package yueyong.salarymanagement.module.business.salary.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yueyong.salarymanagement.common.domain.ResponseDTO;
import yueyong.salarymanagement.module.business.salary.dao.UserCompanyDao;
import yueyong.salarymanagement.module.business.salary.domain.dto.UserCompanySettingDTO;
import yueyong.salarymanagement.module.business.salary.domain.entity.UserCompanyEntity;
import yueyong.salarymanagement.module.business.salary.domain.vo.UserCompanyVO;
import yueyong.salarymanagement.module.system.login.domain.RequestTokenBO;
import yueyong.salarymanagement.util.SmartBeanUtil;
import yueyong.salarymanagement.util.SmartRequestTokenUtil;

import java.util.List;

/**
 * Copyright: yueyong (c) 2022
 *
 * @author he.shaorong
 * @version 1.0
 * @date 2022/2/3
 */
@Service
public class UserCompanyService {

    @Autowired
    private UserCompanyDao userCompanyDao;


    public List<UserCompanyVO> queryUserCompany() {
        RequestTokenBO requestToken = SmartRequestTokenUtil.getRequestUser();
        Long uid = requestToken.getRequestUserId();
        return userCompanyDao.getUserCompanyByUid(uid);
    }

    public ResponseDTO addOrUpdateCompany(UserCompanySettingDTO userCompanySettingDTO) {
        RequestTokenBO requestToken = SmartRequestTokenUtil.getRequestUser();
        Long uid = requestToken.getRequestUserId();
        List<UserCompanyEntity> resultList = userCompanyDao.getUserCompanyEntityByByUid(uid);

        if (resultList == null || resultList.size() == 0) {
            UserCompanyEntity userCompanyEntity = SmartBeanUtil.copy(userCompanySettingDTO, UserCompanyEntity.class);
            userCompanyEntity.setUid(uid);
            userCompanyEntity.setCompanyId(0);
            userCompanyDao.insert(userCompanyEntity);
        } else {
            UserCompanyEntity userCompanyEntity = SmartBeanUtil.copy(userCompanySettingDTO, UserCompanyEntity.class);
            userCompanyEntity.setUid(uid);
            userCompanyEntity.setId(resultList.get(0).getId());
            userCompanyEntity.setCompanyId(0);
            userCompanyDao.updateById(userCompanyEntity);
        }

        return ResponseDTO.succ();
    }
}
